const { validationResult, buildCheckFunction } = require('express-validator');
const Error = require('../config/error-code')

// 采用并行处理
// module.exports已经导出一个函数了，使用exports重新指向它
exports = module.exports = validations => {
    return async (req, res, next) => {
        await Promise.all(validations.map(validation => validation.run(req)));

        const errors = validationResult(req);
        console.log(errors);
        if (errors.isEmpty()) {
            return next();
        }
        res.status(200).json({
            success:false,
            errcode:errors.array()[0].msg.errcode || Error['XP003'].errcode,
            errmsg:errors.array()[0].msg.errmsg || Error['XP003'].errmsg,
            data:{}
        });
    };
};
